<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="loading" />
    <!-- /加载中 loading -->

    <!-- 404 -->
    <div class="error" v-if="is404">
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 404 -->

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <!-- <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="hSwitchFollowed()">{{
          article.is_followed ? "取消关注" : "+ 关注"
        }}</van-button>
      </div> -->

      <!-- 作者信息封装成一个组件， 父传子把文章详情传到子组件中 -->
      <author-info :author="article">
        <!-- van-button 会放到子组件中的插槽slot中 -->
        <van-button round size="small" type="info" @click="hSwitchFollowed()">{{
          article.is_followed ? "取消关注" : "+ 关注"
        }}</van-button>
      </author-info>

      <!-- 文章内容 -->
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>

      <!-- 点赞/不喜欢 -->
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="hSwitchLike"
          :disabled="this.article.attitude === 0"
          >{{ article.attitude === 1 ? "取消点赞" : "点赞" }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="hSwitchDislike"
          :disabled="this.article.attitude === 1"
          >{{ article.attitude === 0 ? "取消不喜欢" : "不喜欢" }}</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <article-comment :articleId="articleId"></article-comment>
    <!-- 文章评论 -->
  </div>
</template>

<script>
import { getDetail } from '@/api/article.js'
import AuthorInfo from '@/components/author.vue'
import ArticleComment from './comment.vue'

import {
  followed,
  unFollowed,
  like,
  unLike,
  dislike,
  unDislike
} from '@/api/user.js'
export default {
  name: 'ArticleIndex',
  components: { AuthorInfo, ArticleComment },
  data () {
    return {
      articleId: '', // 文章id 传递给子组件 comment.vue
      is404: false, // 文章是否存在
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  methods: {
    // 加载详情列表页
    async loadGetDetail () {
      try {
        this.is404 = false
        this.loading = true
        const res = await getDetail(this.$route.params.id)
        // console.log('getDetail', res)
        this.article = res.data.data
        this.loading = false
      } catch (err) {
        // 若文章不存在 err中的配置中会返回404  err.response.status = 404
        // console.dir(err)
        if (err.response.status === 404) {
          this.is404 = true
          // 取消加载状态
          this.loading = false
        }
      }
    },
    // 处理用户关注问题
    async hSwitchFollowed () {
      const { is_followed: isFollowed, aut_id: autId } = this.article
      try {
        if (isFollowed) {
          // 已经关注了，要取消关注
          await unFollowed(autId)
          this.article.is_followed = false
          this.$toast.success('取消关注')
        } else {
          // 没有关注，要+关注
          await followed(autId)
          this.article.is_followed = true
          this.$toast.success('关注成功')
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    },
    // 处理点赞问题
    async hSwitchLike () {
      try {
        const { art_id: artId, attitude } = this.article
        console.log(artId.toString())
        if (attitude === -1) {
          // 无态度状态， 需要点赞
          await like(artId.toString())
          this.article.attitude = 1
          this.$toast.success('点赞成功')
        } else {
          // 点赞状态， 需要改为无状态
          await unLike(artId.toString())
          this.article.attitude = -1
          this.$toast.success('取消点赞')
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    },
    // 处理不喜欢问题
    async hSwitchDislike () {
      try {
        const { art_id: artId, attitude } = this.article
        if (attitude === -1) {
          // 无态度状态， 需要不喜欢
          await dislike(artId.toString())
          this.article.attitude = 0
          this.$toast.success('不喜欢成功')
        } else {
          // 不喜欢状态， 需要改为无状态
          await unDislike(artId.toString())
          this.article.attitude = -1
          this.$toast.success('取消不喜欢')
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  },
  created () {
    // 传递给子组件 comment.vue
    this.articleId = this.$route.params.id
    this.loadGetDetail()
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
